package com.example.zg.androidnewview;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.zg.androidnewview.fragments.AppFragment1;
import com.example.zg.androidnewview.fragments.Fragment1;
import com.example.zg.androidnewview.fragments.Fragment2;
import com.example.zg.androidnewview.fragments.Fragment3;

import java.util.ArrayList;
import java.util.List;

public class ChangeConfigActivity extends AppCompatActivity {
    private static final String TAG = "ChangeConfigActivity";
    private TabLayout mTabLayout;
    private String[] mTitles={"书架","精品","发现"};
    private List<Fragment> mFragments;
    private Fragment mCurFragment;
    private FragmentManager mFgManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_config);
        Log.i(TAG, "onCreate: ");

//            initView();
//            setCurrentFragment(0);
    }

    /*private void initView(){
        mTabLayout=findViewById(R.id.tabLayout1);
        for(int i=0;i<mTitles.length;i++){
            mTabLayout.addTab(mTabLayout.newTab().setText(mTitles[i]));
        }
        mFragments=new ArrayList<>();
        mFragments.add(new Fragment1());
        mFragments.add(new Fragment2());
        mFragments.add(new Fragment3());
        mFgManager=getSupportFragmentManager();

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setCurrentFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void setCurrentFragment(int index){
        Log.i(TAG, "setCurrentFragment: index");
        FragmentTransaction ft = mFgManager.beginTransaction();

        if(mCurFragment!=null && !mCurFragment.isHidden()){
            ft.hide(mCurFragment);
        }

        Fragment fragment = mFragments.get(index);
        if(fragment.isAdded()){
            ft.show(fragment);

        }else{
            ft.add(R.id.layout,fragment,fragment.getClass().getSimpleName());
        }
        mCurFragment=fragment;
        ft.commit();
    }*/

    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        super.onDestroy();
    }

    public void hide(View view) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        Fragment fragment=manager.findFragmentByTag(AppFragment1.class.getSimpleName());
        if(fragment!=null){
            if(!fragment.isHidden()){
                Log.i(TAG, "hide: 隐藏");
                ft.hide(fragment);
            }else{
                Log.i(TAG, "hide: 已经隐藏了,不要重复隐藏");
            }
        }else{
            Log.i(TAG, "hide: fragment 不存在");
        }
        ft.commit();
    }

    public void show(View view) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        Fragment fragment=manager.findFragmentByTag(AppFragment1.class.getSimpleName());
        if(fragment==null){
            Log.i(TAG, "show: 新建 Fragment1");
            fragment=new AppFragment1();
        }else{
            Log.i(TAG, "show: 找到 Fragment1");
        }
        if(fragment.isAdded()){
            if(fragment.isHidden()){
                Log.i(TAG, "show: 显示");
                ft.show(fragment);
            }
        }else{
            ft.add(R.id.layout,fragment,fragment.getClass().getSimpleName());
            Log.i(TAG, "show: 添加");
        }
        ft.commit();

    }
}
